//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
 * Copyright (c) 1997-1998 University of Utah and the Flux Group.
 * All rights reserved.
 *
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 *
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
#include <elatypes.h>
#include <osenv.h>
#include <xf86.h>
#include <fb.h>
#include "buf_io.h"
#include "gdivga.h"

extern unsigned char defaultDAC[768];
extern pointer vgaLinearBase;
extern ScrnInfoRec SvgaInfoRec;

extern void   Video_WriteDAC(int index, int r, int g, int b);
extern void   Video_ReadDAC(int index, int *r, int *g, int *b);

bufio_t *
zee_video_init_framebuffer(DisplayModePtr p)
{
    bufio_t *fb;

    /* this memory alloc will not use.*/
    /*if((fb = (bufio_t *)Xalloc(sizeof(*fb))) == NULL )
        return NULL;
    */

    fb = bufio_create((_ELASTOS UInt32)vgaLinearBase,
        SvgaInfoRec.videoRam);
    fb->width = p->HDisplay;
    fb->height = p->VDisplay;
    fb->depth = SvgaInfoRec.bitsPerPixel;
    fb->video_mem = SvgaInfoRec.videoRam;
    fb->stride = fb->width;                /* XXX */

    return fb;
}


int
zee_video_cmap_write(zee_cmap_entry_t *c)
{
    Video_WriteDAC(c->index, c->red, c->green, c->blue);
    return 1;
}

int
zee_video_cmap_writeDefaultDac(void)
{
    int i;

    for ( i = 0; i<=255; i++)
        Video_WriteDAC(i, defaultDAC[i*3]<<2, defaultDAC[i*3+1]<<2, defaultDAC[i*3+2]<<2);

    return 1;
}

int
zee_video_cmap_read(zee_cmap_entry_t *c)
{
    Video_ReadDAC(c->index, &(c->red), &(c->green), &(c->blue));
    /* Assume an 8bit dac */
    c->maxi = 255;

    return 1;
}

int
zee_video_cmap_fg_index(void)
{
    return 0;
}


